set matsize 5000
pwd

use ${clean_data}/taxtax_fullyr_clean if (age==23 | age==26 ) ///
      & (inrange(taxyr_cut,1980,1981) | inrange(taxyr_cut,1985,1986) | inrange(taxyr_cut,1990,1991)), clear

**ADJUST FOR LEAP YEAR**
replace z = . if month(dob)==2 & day(dob)==29
replace z = z-1 if  inrange(month(dob),3,6) & (year(dob)==1980 |  year(dob)==1984 | year(dob)==1988 | year(dob)==1992)


***********************************
** PARAMETERS
***********************************
global zs = "-61 59 90 151 31 -153 -92 120"


global outcomes = "wagez3"
global ages = "23 26"
global samples = "k1elig"

local restrict = "all"
local predsamp = "predL12"
local covar = "tyrc"
local tyrc = "1979_1991"
local donut = "8"
local wday = "20"
local sch_cut = "dropany_miss"


** REGRESSIONS
***********************************
tempname memhold1	//	where you temporarily hold the postings
tempfile rdest1		//	name of the temporary file that will become the new dataset
postfile `memhold1' str30(outcome age sample)  z0 dob0 b se str750(command) using `rdest1'


foreach age of global ages {
	foreach y of global outcomes {
		foreach samp of global samples {
			disp ""
			disp "************************************************************************************"
			disp "** OUTCOME: `y'  \\ AGE: `age' (${a_`age'}) \\ WDAY: `wday' \\ DONUT: `donut' **"
			disp "** SAMP: `samp' (${r_`samp'})  \\ PREDSAMP: `predsamp' (${rp_`predsamp'}) \\ RESTRICT: `restrict' (${r_`restrict'})
      disp "** SCH_CUT: `sch_cut' (${sc_`sch_cut'}) **"
			disp "************************************************************************************"
			disp ""

			foreach z0 of global zs {
        summ dob if z==`z0'
        local dob_min = r(min)
        if mod(`z0',30)==0  disp "Placebos: `z0'  --" %tdnn/dd/CCYY `dob_min' _newline
        else disp "." _continue

				capture drop *_pbo
        qui gen z_pbo = z - `z0'
				qui gen bcut_pbo = (z_pbo<0)

				qui reghdfe `y' i.bcut_pbo##c.z_pbo ${x_`covar'} ///
            if ${tyrcs_`tyrc'} &inrange(z_pbo,-`wday'-`donut',`wday'+`donut'-1)& !inrange(z_pbo,-`donut',`donut'-1)  ///
              ${r_`samp'}  ${r_`restrict'} ${a_`age'} ${sc_`sch_cut'} ${rp_`predsamp'}, absorb(${fe_`covar'}) vce(${sehdfe_clust_z})
				local command=e(cmdline)

				post `memhold1' ("`y'") ("`age'") ("`samp'") (`=`z0'') (`=`dob_min'') (`=_b[1.bcut_pbo]') (`=_se[1.bcut_pbo]') ("`command'")
			}
		}
	}
}

postclose `memhold1'	//	end the postfile

use `rdest1', clear
save ${results}/placebos, replace
